package com.hellogood.yaohuo.server.mgr;

import java.math.BigDecimal;
import java.util.List;

import org.apache.log4j.Logger;

import com.hellogood.yaohuo.server.DateUtil;
import com.hellogood.yaohuo.server.dao.factory.DAOFactory;
import com.hellogood.yaohuo.shared.entity.Detail;
import com.hellogood.yaohuo.shared.entity.Form;
import com.hellogood.yaohuo.shared.entity.Log;
import com.hellogood.yaohuo.shared.entity.YKDetail;
import com.hellogood.yaohuo.shared.entity.YKForm;
import com.hellogood.yaohuo.shared.exception.DatabaseException;
import com.hellogood.yaohuo.shared.vo.MainFrame;

public class YikuMgr {
	Logger log = Logger.getLogger(this.getClass());
	private String tableFormName = "YkForm";
	private String tableDetailName = "YkDetail";
	private String billNoField = "YkBillNo";
	private String dateField = "YkDate";

	public String createYikuForm(YKForm form) {
		execute(form);
		String result = DAOFactory.getInstance().createYKFormDAO().createYiku(form, false);
		if (result.equals("")) {
			addLog(form.getYkBillNo(), false);
		}
		return result;
	}

	public String modifyYikuForm(YKForm form) {
		execute(form);
		String result = DAOFactory.getInstance().createYKFormDAO().createYiku(form, true);
		if (result.equals("")) {
			addLog(form.getYkBillNo(), true);
		}
		return result;
	}

	private void execute(YKForm form) {
		List<YKDetail> list = form.getList();
		int totalCounts = 0;
		BigDecimal formCost = new BigDecimal(0);
		BigDecimal formTotal = new BigDecimal(0);

		for (YKDetail d : list) {
			BigDecimal cost = d.getjPrice().multiply(new BigDecimal(d.getCounts()));
			BigDecimal total = d.getPrice().multiply(new BigDecimal(d.getCounts()));
			d.setCost(cost);
			d.setTotal(total);
			totalCounts += d.getCounts();
			formCost = formCost.add(cost);
			formTotal = formTotal.add(total);
		}
		form.setOutOrgCode("C001");
		form.setInOrgCode("C002");
		form.setCounts(totalCounts);
		form.setCost(formCost);
		form.setNetCost(formCost);
		form.setTotal(formTotal);

	}

	private void addLog(String billNo, boolean isModify) {
		int logID = DAOFactory.getInstance().createLogDAO().getLastLogNo();
		logID++;
		Log log = new Log();
		log.setLogId(logID);
		log.setStaffCode(MainFrame.currentUser.getStaffCode());
		log.setStaffName(MainFrame.currentUser.getStaffName());
		log.setLogDate(DateUtil.currentTime());
		if (isModify) {
			log.setContent("修改移库单" + billNo);
		} else {
			log.setContent("新建移库单" + billNo);
		}
		DAOFactory.getInstance().createLogDAO().addNewLog(log);
	}

	public int getLastYKBillNo(String billNo) {
		String no = DAOFactory.getInstance().createYKFormDAO().getLastBillNo(billNo, tableFormName, billNoField);
		if (no != null) {
			// 序号为最后两位 格式为"WLYH组织号YYYYMMDDXX" XX为序号
			if (no.length() > 2) {
				String lastNo = no.substring(no.length() - 2, no.length());
				return Integer.valueOf(lastNo);
			}
		}
		return 0;
	}

	public List<Form> searchPsThForm(String code, int level, String startDate, String endDate, String rz) {
		if (code.equals("0")) {
			level = 1;
		}
		if (level == 2) {
			return DAOFactory.getInstance().createYKFormDAO().searchFormByQuyu(tableFormName, billNoField, dateField, code, startDate, endDate, rz);
		} else if (level == 3) {
			return DAOFactory.getInstance().createYKFormDAO().searchForm(tableFormName, billNoField, dateField, code, startDate, endDate, rz);
		} else if (level == 1) {
			return DAOFactory.getInstance().createYKFormDAO().searchFormByQuyu(tableFormName, billNoField, dateField, null, startDate, endDate, rz);
		}
		return null;
	}

	public Form getFormDetail(String billNo) {
		Form form = DAOFactory.getInstance().createYKFormDAO().getFormByBillNo(tableFormName, billNoField, dateField, billNo);
		if (form != null) {
			List<Detail> list = DAOFactory.getInstance().createYKDetailDAO().getDetailByBillNo(billNo, tableDetailName, billNoField);
			form.setList(list);
		}
		return form;
	}

	public String deletePeisongTuihuo(String billNo) {
		return DAOFactory.getInstance().createPsThFormDAO().deleteForm(tableFormName, tableDetailName, billNoField, billNo);
	}

	public String rz(List<String> bills) {
		String result = "";
		try {
			for (String bill : bills) {
				DAOFactory.getInstance().createPsThFormDAO().rz(bill);
			}
		} catch (DatabaseException e) {
			log.error(e.getMessage());
			e.printStackTrace();
			result = e.getMessage();
		}
		return result;
	}
}
